//
//  ViewController.m
//  GroupFoldDemo
//
//  Created by lantaiyuan on 16/9/20.
//  Copyright © 2016年 youmy. All rights reserved.
//  换乘详情

#import "ViewController.h"
#import "YMSectionModel.h"

#import "YMWalkSectionHeaderView.h"
#import "YMBusSectionHeaderView.h"
#import "YMStationSectionHeaderView.h"
#import "YMStartSectionHeaderView.h"
#import "YMEndSectionHeaderView.h"

#import "YMStationCell.h"

#import "MJExtension.h"

#import "UIColor+Extension.h"

static NSString * identifier = @"identifier";

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic, strong) NSMutableArray * dataSource;
@end

@implementation ViewController

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor colorWithHexString:@"#f5f5f5"];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (void)test{
    
    NSString * path = [[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:@"transfer.json"];
    NSData * data = [NSData dataWithContentsOfFile:path];
    NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];

    NSArray * array = [YMSectionModel mj_objectArrayWithKeyValuesArray:dic[@"result"][@"transfer"]];
    [self.dataSource addObjectsFromArray:array];
    
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"YMStationCell" bundle:nil] forCellReuseIdentifier:identifier];
    
    [self test];
    
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    YMSectionModel * model = self.dataSource[section];
    return model.isExpanded ? model.stations.count : 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YMStationCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    YMSectionModel * model = self.dataSource[indexPath.section];
    NSArray * arr = model.stations;
    cell.stationLabel.text = arr[indexPath.row];
    return cell;
}


#pragma mark - UITableViewDelegate
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    YMSectionModel * model = self.dataSource[section];
    
    __weak typeof(self) weakSelf = self;
    
    // 起点
    if ([model.type isEqualToString:@"start"]) {
        YMStartSectionHeaderView * start = [[YMStartSectionHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
        start.nameLabel.text = model.name;
        return start;
    }
    
    // 终点
    if ([model.type isEqualToString:@"end"]) {
        YMEndSectionHeaderView * end = [[YMEndSectionHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
        end.nameLabel.text = model.name;
        return end;
    }
    
    // 步行
    if ([model.type isEqualToString:@"walk"]) {
        YMWalkSectionHeaderView * walk = [[YMWalkSectionHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
        walk.nameLabel.text = model.name;
        walk.callBack = ^{
            UIViewController * walkingDetailVc = [[UIViewController alloc] init];
            walkingDetailVc.view.backgroundColor = [UIColor whiteColor];
            walkingDetailVc.navigationItem.title = @"步行";
            [weakSelf.navigationController pushViewController:walkingDetailVc animated:YES];
        };
        return walk;
    }
    
    // 最近的站点
    if ([model.type isEqualToString:@"bus"]) {
        YMBusSectionHeaderView * bus = [[YMBusSectionHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 185)];
        bus.sectionModel = model;
        bus.expandCallBack = ^(BOOL isExpanded) {
            
            model.isExpanded = isExpanded;
            
            [weakSelf.dataSource replaceObjectAtIndex:section withObject:model];
            
            [tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationNone];
        };
        
        bus.lookDetailBlock = ^{
            UIViewController * walkingDetailVc = [[UIViewController alloc] init];
            walkingDetailVc.view.backgroundColor = [UIColor whiteColor];
            walkingDetailVc.navigationItem.title = @"查看详情";
            [weakSelf.navigationController pushViewController:walkingDetailVc animated:YES];
        };
        return bus;
    }
    
    // 站点
    if ([model.type isEqualToString:@"station"]) {
        YMStationSectionHeaderView * station = [[YMStationSectionHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
        station.nameLabel.text = model.name;
        return station;
    }
    
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 24;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    YMSectionModel * model = self.dataSource[section];
    return [model.type isEqualToString:@"bus"] ? 185 : 44;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.000001;
}
@end
